//
//  ItemFirstViewController.m
//  Lovelife
//
//  Created by apple on 15/4/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ItemFirstViewController.h"
#import "ItemFirstModel.h"
#import "AFNetworking.h"
#import "ItemFirstThreeCanTitleModel.h"
#import "ItemFirstThressCanDetasilsModel.h"
#import "ItemFirstMealsTitleCell.h"
#import "SDCycleScrollView.h"
#import "UIImageView+WebCache.h"
#import "ItemFirstWuAndWanViewController.h"
#import "ItemFirstZaoItemViewController.h"
#import "MJRefresh.h"
#import "NightManager.h"

@interface ItemFirstViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,retain)NSMutableArray *modelArr;//大标题 轮播图使用

@property (nonatomic,retain)NSMutableArray *breakfastTitleModelArr;//早餐标题

@property (nonatomic,retain)NSMutableArray *lunchTitleModelArr;//午餐标题

@property (nonatomic,retain)NSMutableArray *dinnerTitleModelArr;//晚餐标题

@property (nonatomic,retain)NSMutableArray *breakfastDetailsModelArr;//早餐详情

@property (nonatomic,retain)NSMutableArray *lunchDetailsModelArr;//午餐详情

@property (nonatomic,retain)NSMutableArray *dinnerDetailsModelArr;//晚餐详情

@property (nonatomic,retain)UICollectionView *titleCollectionView;//cell:三餐标题概括部分
@property (nonatomic,retain)NSMutableArray *imagesURL;//轮播图图片数组

@property (nonatomic,retain)SDCycleScrollView *cycle;//轮播图

@property (nonatomic,retain)UIActivityIndicatorView *acIntor;//网络延迟轮





@end

@implementation ItemFirstViewController
-(void)dealloc
{
    [_acIntor release];
    [_cycle release];
    [_imagesURL release];
    [_titleCollectionView  release];
//    [_dinnerDetailsModelArr release];
//    [_dinnerTitleModelArr release];
//    [_lunchDetailsModelArr release];
//    [_breakfastDetailsModelArr release];
//    [_breakfastTitleModelArr release];
//    [_lunchTitleModelArr release];
    [_modelArr release];
    [super dealloc];


}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
        self.modelArr = [NSMutableArray array];
        
        self.breakfastTitleModelArr = [NSMutableArray array];
        
        self.breakfastDetailsModelArr = [NSMutableArray array];
        
        self.lunchTitleModelArr = [NSMutableArray array];
        
        self.lunchDetailsModelArr = [NSMutableArray array];
        
        self.dinnerTitleModelArr = [NSMutableArray array];
        
        self.dinnerDetailsModelArr = [NSMutableArray array];
        
        self.imagesURL = [NSMutableArray array];
        

    }
    
    return self;


}
 
-(void)getData
{
 
        [self.acIntor startAnimating];

    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.plan.detail.php?random=%d",arc4random()%(30)+1] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
       //解析第一层 创建轮播图使用
        ItemFirstModel *first = [[ItemFirstModel alloc]init];
        
        [first setValuesForKeysWithDictionary:responseObject];
        
        [self.modelArr addObject:first];
        //轮播图url图片
        [self.imagesURL addObject:[NSURL URLWithString:first.thumb]];
        
        NSLog(@"first.thumb is %@",first.thumb);
        NSLog(@"first.title is %@",first.title);
    
        
        [first release];
        //解析第二层 三餐标题简介
        
        ItemFirstThreeCanTitleModel *breakfastTitle = [[ItemFirstThreeCanTitleModel alloc]init];
        [breakfastTitle setValuesForKeysWithDictionary:responseObject[@"zaocan"]];
        
        [self.breakfastTitleModelArr addObject:breakfastTitle];
        
        [breakfastTitle release];//早餐
        
        [self.imagesURL addObject:[NSURL URLWithString:breakfastTitle.thumb]];
        
        
        
        
        ItemFirstThreeCanTitleModel *lunchTitle = [[ItemFirstThreeCanTitleModel alloc]init];
        
        [lunchTitle setValuesForKeysWithDictionary:responseObject[@"wucan"]];
        
        [self.lunchTitleModelArr addObject:lunchTitle];
        
        
        [self.imagesURL addObject:[NSURL URLWithString:lunchTitle.thumb]];
        
        [lunchTitle release];//午餐
        
        
        
        
        ItemFirstThreeCanTitleModel *dinnerTitle = [[ItemFirstThreeCanTitleModel alloc]init];
        
        [dinnerTitle setValuesForKeysWithDictionary:responseObject[@"wancan"]];
        
        [self.dinnerTitleModelArr addObject:dinnerTitle];
        
        [self.imagesURL addObject:[NSURL URLWithString:dinnerTitle.thumb]];
        
        [dinnerTitle release];//晚餐
        
       ;
      

        //解析第三层 三餐详情

        NSMutableArray *zaoTempArr = responseObject[@"zaocan"][@"chi"];
        
        for (NSMutableDictionary *dic in zaoTempArr) {
            
              ItemFirstThressCanDetasilsModel *breakfastDetails = [[ItemFirstThressCanDetasilsModel alloc]init];
            
            [breakfastDetails setValuesForKeysWithDictionary:dic];

            [self.breakfastDetailsModelArr addObject:breakfastDetails];
            
            
            
            [breakfastDetails release];
        }//早餐
        
        
        NSMutableArray *wuTempArr = responseObject[@"wucan"][@"chi"];
        
        for (NSMutableDictionary *dic in wuTempArr) {
            
            ItemFirstThressCanDetasilsModel *lunchDetails = [[ItemFirstThressCanDetasilsModel alloc]init];
            
            [lunchDetails setValuesForKeysWithDictionary:dic];
            
            [self.lunchDetailsModelArr addObject:lunchDetails];
            
            [lunchDetails release];
        }//午餐
        
        
        NSMutableArray *wanTempArr = responseObject[@"wancan"][@"chi"];
        
        for (NSMutableDictionary *dic in wanTempArr) {
            
            ItemFirstThressCanDetasilsModel *dinnerDetails = [[ItemFirstThressCanDetasilsModel alloc]init];
            
            [dinnerDetails setValuesForKeysWithDictionary:dic];
            
            [self.dinnerDetailsModelArr addObject:dinnerDetails];
            
            
            
            [dinnerDetails release];
            
            
        }//晚餐
        
 
            [self.acIntor stopAnimating];
            [self createView];

        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错误信息%@",error);
    }];
    


}
//创建轮播图
-(void)createSDCycleScrollView
{

    self.cycle = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/4+50) imageURLsGroup:nil];
    self.cycle.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    self.cycle.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    [self.view addSubview:self.cycle];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.cycle.imageURLsGroup = self.imagesURL;
    });
    

}

- (void)createView //创建视图collectionView cell：三餐标题
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    
    flow.itemSize = CGSizeMake(self.view.frame.size.width-50, self.view.frame.size.height/6+35);
    flow.minimumLineSpacing = 5;
    
    flow.minimumInteritemSpacing = 1;
    
    flow.sectionInset = UIEdgeInsetsMake(15, 10, 10, 10);
    
    flow.headerReferenceSize =CGSizeMake(self.view.frame.size.width, self.view.frame.size.height/4+50);
    
    self.titleCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    
    self.titleCollectionView.backgroundColor = [UIColor colorWithRed:245/255.0 green:247/255.0 blue:231/255.0 alpha:0.5];

    [self.view addSubview:self.titleCollectionView];
    
    [_titleCollectionView release];
    
    [flow release];
    
    self.titleCollectionView.dataSource = self;
    
    self.titleCollectionView.delegate = self;
    
    
    
    [self.titleCollectionView registerClass:[ItemFirstMealsTitleCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self.titleCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
//    [self.titleCollectionView addHeaderWithCallback:^{
//        [self.breakfastTitleModelArr removeAllObjects];
//        [self.lunchTitleModelArr removeAllObjects];
//        [self.dinnerTitleModelArr removeAllObjects];
//        [self.titleCollectionView reloadData];
//        [self getData];
//        self.temp = NO;
//        [self.titleCollectionView headerEndRefreshing];
//    }];
//    
//    [self.titleCollectionView addFooterWithCallback:^{
//        
//        self.number ++;
//        self.temp = NO;
//        
//        [self getData];
//        [self.titleCollectionView footerEndRefreshing];
//
//        
//        
//    }];
    
    
    
    

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return 3;

}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    ItemFirstMealsTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        ItemFirstThreeCanTitleModel *zaoTitle = self.breakfastTitleModelArr[0];
        
        [cell.Img sd_setImageWithURL:[NSURL URLWithString:zaoTitle.thumb]];
        
        cell.titleLB.text = zaoTitle.title;

        
    }else if (indexPath.row == 1 )
    {
        ItemFirstThreeCanTitleModel *wuTtile = self.lunchTitleModelArr[0];
        
        [cell.Img sd_setImageWithURL:[NSURL URLWithString:wuTtile.thumb]];
        
        cell.titleLB.text = wuTtile.title;
        
        
    
    
    }else if (indexPath.row == 2)
    {
        ItemFirstThreeCanTitleModel *wanTtile = self.dinnerTitleModelArr[0];
        
        [cell.Img sd_setImageWithURL:[NSURL URLWithString:wanTtile.thumb]];
        
        cell.titleLB.text = wanTtile.title;
        

    
    }
    return cell;
   

}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView * head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];

        [self createSDCycleScrollView];
        
        [head addSubview:self.cycle];
        
        return head;
    }else
    {
        return 0;
    
    }

}

-(void)createAcIntor
{
    self.acIntor = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.acIntor setCenter:self.view.center];
    [self.acIntor setBackgroundColor:[UIColor grayColor]];
    [self.acIntor setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.acIntor];
    [_acIntor release];

}

- (void)viewDidLoad {
    [super viewDidLoad];
   [self.view setBackgroundColor:[UIColor whiteColor]];
#pragma mark 创建网路延迟转轮
    
    [self createAcIntor];
    [self getData];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-shuaxin"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    
    
}

-(void)rightBarButtonItemAction:(UIBarButtonItem *)sander
{
    [self getData];
    [self.titleCollectionView reloadData];

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        ItemFirstZaoItemViewController *zao = [[ItemFirstZaoItemViewController alloc]init];
        
        zao.zaoModelArr = self.breakfastDetailsModelArr;
        
        [self.navigationController pushViewController:zao animated:YES];
        
        [zao release];
        
    }else if (indexPath.row == 1)
    {
    
        ItemFirstWuAndWanViewController *wu = [[ItemFirstWuAndWanViewController alloc]init];
        
        wu.wuModelArr = self.lunchDetailsModelArr;
        
        [self.navigationController pushViewController:wu animated:YES];
        
        [wu release];
    
    }else
    {
    
        ItemFirstWuAndWanViewController *wan = [[ItemFirstWuAndWanViewController alloc]init];
        
        wan.wuModelArr = self.dinnerDetailsModelArr;
        
        [self.navigationController pushViewController:wan animated:YES];
        
        [wan release];
    
    }


}

-(void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];

}

-(void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
